package com.huynhchihieu.dao;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.huynhchihieu.domain.Role;
import com.huynhchihieu.domain.UserRole;

@Repository
public class UserRoleDaoImpl implements UserRoleDao{
	
	@Autowired
	RoleDAO roleDAO;

	@PersistenceContext(unitName = "persistenceUnit")
	private EntityManager entityManager;
	public void createUserRole(int user_id, int role_id) {
		Role role = roleDAO.getRole(role_id);
		StringBuilder descriptionRole = new StringBuilder("ROLE_");
		descriptionRole.append(role.getRole().toUpperCase());
		 entityManager.createNativeQuery("insert into user_role (user_id, role_id, description) values (?, ?, ?)"
				, UserRole.class).setParameter(1, user_id)
				.setParameter(2, role_id)
				.setParameter(3, descriptionRole.toString()).executeUpdate(); 
	}
	
}
